package com.boardour.toupin;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.boardour.base.ui.AppActivity;
import com.boardour.base.ui.titlebar.DefTitleBar;
import com.boardour.base.utils.SPUtils;
import com.boardour.base.utils.TSUtils;
import com.boardour.toupin.push.PushActivity;
import com.boardour.toupin.show.ShowActivity;

import cn.carhouse.views.XEditLayout;

/**
 * 登录页面
 */
public class LoginActivity extends AppActivity {
    private XEditLayout xetName, xetPass;
    private Button btnLogin;
    private ProgressBar progressBar;

    @Override
    protected void initTitle(DefTitleBar titleBar) {
        titleBar.setTitle("投屏接收端登录");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initViews(View view) {
        xetName = findViewById(R.id.et_name);
        xetPass = findViewById(R.id.et_pass);
        btnLogin = findViewById(R.id.btn_login);
        progressBar = findViewById(R.id.pb);
        // 设置保存
        final String name = SPUtils.getString("name");
        String pass = SPUtils.getString("pass");
        if (!TextUtils.isEmpty(name)) {
            xetName.setEditText(name);
            final EditText etName = xetName.getEditContent();
            etName.post(new Runnable() {
                @Override
                public void run() {
                    // 将光标移动到最后
                    if (etName.isFocused()) {
                        etName.setSelection(name.length());
                    }
                }
            });
        }
        if (!TextUtils.isEmpty(pass)) {
            xetPass.setEditText(pass);
        }
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }

    /**
     * 登录
     */
    private void login() {
        String name = xetName.getEditText();
        if (TextUtils.isEmpty(name)) {
            EditText etName = this.xetName.getEditContent();
            TSUtils.show(etName.getHint().toString());
            etName.requestFocus();
            return;
        }

        String pass = xetPass.getEditText();
        if (TextUtils.isEmpty(pass)) {
            EditText etPass = this.xetPass.getEditContent();
            TSUtils.show(etPass.getHint().toString());
            etPass.requestFocus();
            return;
        }
        progressBar.setVisibility(View.VISIBLE);
        closeKeyBord();
        progressBar.postDelayed(new Runnable() {
            @Override
            public void run() {
                progressBar.setVisibility(View.GONE);
                // 密码：123456
                if (pass.equals("123456")) {
                    SPUtils.putString("name", name);
                    SPUtils.putString("pass", pass);
                    TSUtils.show("登录成功");
                    loginSucceed();
                } else {
                    TSUtils.show("名称或者密码不正确");
                }
            }
        }, 800);

    }

    /**
     * 登录成功
     */
    private void loginSucceed() {
        show();
    }

    private void push() {
        startActivity(new Intent(this, PushActivity.class));
    }

    private void show() {
        startActivity(new Intent(this, ShowActivity.class));
    }
}
